<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewpoint" content="width=device,initial-scale=1">
    <link href="font-awesome-4.7.0/css/font-awesome.css" rel="stylesheet">
    <link href="reset.css" rel="stylesheet">
    <link href="icon.css" rel="stylesheet">
    <link href="login.css" rel="stylesheet">
    <title>饿了么 登陆</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
    
    <style>
        li  {
            display: flex;
        }
        .title {
            font-size: 3.4vw;
        }
    </style>
</head>

<body>
    <div class="wrapper">
        <!--  header部分-->
        <header>
            <p>用户登陆</p>
        </header>
        <!--    表单部分-->
        <ul class="form-box">
            <li>
                <div class="title">
                    手机号码：
                </div>
                <div class="content">
                    <input type="text" placeholder="手机号码" id="userId">
                </div>
            </li>
            <li>
                <div class="title">
                    密码：
                </div>
                <div class="content">
                    <input type="password" placeholder="密码" id="password">
                </div>
            </li>
        </ul>
        <div class="button-login">
            <button id="loginBtn">登陆</button>
        </div>
        <div class="button-register">
            <button onclick="location.href='register.html'">去注册</button>
        </div>
        <!--    底部菜单部分-->
        <ul class="footer">
            <li onclick="location.href='index.html' ">
                <i class="fa fa-home"></i>
                <p>首页</p>
            </li>
            <li onclick="location.href='Message.html'">
                <i class="fa fa-compass"></i>
                <p>消息</p>
            </li>
            <li onclick="location.href='orderList.html' ">
                <i class="fa fa-file-text-o"></i>
                <p>订单</p>
            </li>
            <li onclick="location.href='Person.html' ">
                <i class="fa fa-user-o"></i>
                <p>我的</p>
            </li>
        </ul>

    </div>
</body>
<script>
    $(document).ready(function () {
        $('#loginBtn').click(function () {
            var userId = $('#userId').val();
            var password = $('#password').val();

            if (userId == '') {
                alert('手机号不能为空');
                return;
            }

            if (password == '') {
                alert('密码不能为空');
                return;
            }

            // 假设我们有一个后端 API 来处理注册请求
            // 这里我们用一个简单的模拟来展示
            $.ajax({
                url: 'http://127.0.0.1:8081/user/login', // 替换为实际的注册 API URL
                type: 'POST',
                contentType: 'application/json; charset=UTF-8', // 设置内容类型为 JSON
                data: JSON.stringify({
                    userId: userId,
                    password: password
                }),
                dataType: 'json',
                success: function (res) {
                    if (res.flag) {
                        // 将用户信息转换为 JSON 字符串
                        var userInfoString = JSON.stringify(res.data);

                        // 使用 jQuery Cookie 插件设置 cookie
                        var expirationDate = new Date();
                        expirationDate.setTime(expirationDate.getTime() + (3600 * 1000 * 24)); // 24小时后过期
                        $.cookie('userInfo', userInfoString, { expires: expirationDate, path: '/' });
                        window.location.href = 'index.html';
                    } else {
                        alert(res.message)
                    }
                },
                error: function (xhr, status, error) {
                    alert('登录失败：' + xhr.statusText);
                }
            });
        });
    });
</script>

</html>